package v1

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"simcard/bll"
	"simcard/model"
	"simcard/server/web/middleware"
	"simcard/utils"
)

var SimCard = &simCard{}

func init() {
	RegisterRouter(SimCard)
}

type simCard struct{}

// Init
func (a *simCard) Init(r *gin.RouterGroup) {
	g := r.Group("/simCard", middleware.Auth())
	{
		g.POST("/create", a.create)
		g.POST("/update", a.update)
		g.POST("/list", a.list)
		g.POST("/delete", a.delete)
		g.POST("/detail", a.find)
		g.POST("/serviceDetail", a.serviceDetail)
		g.POST("/sync", a.sync)
		g.POST("/syncCardConsumptionInfo", a.syncCardConsumptionInfo)
		g.POST("/preRecharge", a.preRecharge)
	}
}

// create
func (a *simCard) create(c *gin.Context) {
	var (
		in  = &model.SimCardCreateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		_ = c.Error(err)
		return
	}

	if err = bll.SimCard.Create(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// update
func (a *simCard) update(c *gin.Context) {
	var (
		in  = &model.SimCardUpdateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		_ = c.Error(err)
		return
	}

	if err = bll.SimCard.Update(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// list
func (a *simCard) list(c *gin.Context) {
	var (
		in  = &model.SimCardListRequest{}
		out = &model.SimCardListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		_ = c.Error(err)
		return
	}

	if out, err = bll.SimCard.List(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

// list
func (a *simCard) find(c *gin.Context) {
	var (
		in  = &model.SimCardInfoRequest{}
		out = &model.SimCardInfo{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		_ = c.Error(err)
		return
	}

	if out, err = bll.SimCard.Find(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

// serviceDetail
func (a *simCard) serviceDetail(c *gin.Context) {
	var (
		in  = &model.GetSimCardServiceDetailRequest{}
		out = &model.GetSimCardServiceDetailResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		_ = c.Error(err)
		return
	}

	if out, err = bll.SimCard.GetSimCardServiceDetail(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

func (a *simCard) sync(c *gin.Context) {
	var (
		in  = &model.SyncRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		_ = c.Error(err)
		return
	}

	if err = bll.SimCard.Sync(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

func (a *simCard) syncCardConsumptionInfo(c *gin.Context) {
	var (
		in  = &model.SimCardConsumptionInfoCronRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		_ = c.Error(err)
		return
	}

	if len(in.Ebikes) == 0 && in.DataMode == 0 && in.StrategyType == "" && in.Id == nil && len(in.Ids) == 0 {
		_ = c.Error(fmt.Errorf("参数缺失"))
		return
	}
	in.DataMode = model.SimCardSyncDataPartOf

	if err = bll.SimCard.SyncCardConsumptionInfo(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

func (a *simCard) preRecharge(c *gin.Context) {
	var (
		in  = &model.PreRechargeRequest{}
		err error
	)

	if err = c.ShouldBindJSON(&in); err != nil {
		_ = c.Error(err)
		return
	}

	if err = bll.SimCard.PreRecharge(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// delete
func (a *simCard) delete(c *gin.Context) {
	var (
		in  = &model.SimCardDeleteRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		_ = c.Error(err)
		return
	}

	if err = bll.SimCard.Delete(c.Request.Context(), in); err != nil {
		_ = c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}
